<?php
class StaticLinksModel extends BaseModel implements Model {
    function __construct() {
        parent::__construct("static_links", "id");
        $this->addField("id", Model::TYPE_INT, Model::MOD_PRIMARY);
        $this->addField("href", Model::TYPE_VARCHAR);
        $this->addField("title", Model::TYPE_VARCHAR);
        $this->addField("group", Model::TYPE_VARCHAR);
        $this->addField("linkorder", Model::TYPE_INT);
    }

    public function renderAlias($record) {
        $result = $record;
        return $result;
    }


    public function changelink($params) {
        $query = QueryHelper::update("static_links", array(
            group => $params['groupname'],
            title => $params['title'],
            href => $params['href'],
        ), "id=" . $params['id']);
        Database::executeQuery($query);
    }

    public function getGroup($groupName) {
        $query = "SELECT id, href, title FROM static_links WHERE `group`='$groupName' ORDER BY linkorder";
        $res = Database::executeQuery($query);
        $data = Database::getDatafromResource($res);
        return $data;
    }

    public function reorder($idlist) {
        for ($i = 0; $i < count($idlist); $i++) {
            $query = QueryHelper::update("static_links", array(linkorder => ($i+1)), "id=" . $idlist[$i]);
            Database::executeQuery($query);
        }
    }

    public function savelinktogroup($groupName, $href, $title) {
        $query = "SELECT count(*) AS c FROM static_links WHERE `group`='$groupName'";
        $res = Database::executeQuery($query);
        $count = Database::getSimpleField($res);
        $query = "INSERT INTO static_links (`group`, href, title, linkorder) VALUES ('$groupName', '$href', '$title', " . ($count+1) . ")";
        Database::executeQuery($query);
    }

    public function removelinkfromgroup($groupName, $idlist) {
        $query = "DELETE FROM static_links WHERE id IN (" . implode(",", $idlist) . ")";
        Database::executeQuery($query);
    }

    public function isAuthorized($operation, $subject = "") {
        return true;
//        if (Auth::isAdmin()) { return true; }
/*
        switch ($operation) {
            case "update" :
            case "delete" :
            case "addnew" : {
                if (Auth::canChangeDrivers()) { return true; }
            }
            case "list" : {
                if (Auth::canListDrivers()) { return true; }
            }
        }
        return false;
*/
    }

    public function modifyDatabaseInput($data) {
        return $data;
    }
}

?>
